<template>
  <a-row justify="center">
    <a-col :span="20">
      <a-form :model="searchParams" layout="inline">
        <a-form-item field="title" label="题目：">
          <a-input
            v-model="searchParams.title"
            placeholder="请输入题目名称..."
          />
        </a-form-item>
        <a-form-item field="tags" label="标签：">
          <a-input-tag
            v-model="searchParams.tags"
            placeholder="请输入题目标签..."
          />
        </a-form-item>
        <a-form-item>
          <a-button @click="doSubmit()">查询</a-button>
        </a-form-item>
      </a-form>
      <a-table
        :columns="columns"
        :data="dataList"
        :pagination="{
          showTotal: true,
          pageSize: searchParams.pageSize,
          current: searchParams.current,
          total,
        }"
        @page-change="onPageChange"
      >
        <template #tags="{ record }">
          <a-space wrap>
            <a-tag
              v-for="(tag, index) of record.tags"
              :key="index"
              color="green"
              >{{ tag }}
            </a-tag>
          </a-space>
        </template>
        <template #passRate="{ record }">
          {{
            `${
              record.submitNum ? record.acceptedNum / record.submitNum : "0"
            }% (${record.acceptedNum}/${record.submitNum})`
          }}
        </template>
        <template #createTime="{ record }"
          >{{ moment(record.createTime).format("YYYY-MM-DD") }}
        </template>
        <template #optional="{ record }">
          <a-space>
            <a-button type="primary" @click="doQuestionPage(record)"
              >做题
            </a-button>
          </a-space>
        </template>
      </a-table>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment/moment";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 6,
  current: 1,
  title: "",
  tags: [],
});

/**
 * 修改题目
 */
const router = useRouter();
const doQuestionPage = async (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

/**
 * 获取题目数据
 */
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

watchEffect(() => {
  loadData();
});

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loadData();
};

/**
 * 改变页码
 * @param page
 */
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});
const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "题目标题",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "passRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    slotName: "optional",
  },
];
</script>

<style></style>
